package com.xz.courseschedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xz.courseschedule.common.vo.BaseVo;
import com.xz.courseschedule.common.vo.SemesterVo;
import com.xz.courseschedule.entity.Semester;
import com.xz.courseschedule.mapper.SemesterMapper;
import com.xz.courseschedule.service.SemesterService;
import com.xz.courseschedule.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zys
 * @CreateTime: 2025-05-22
 */

@Service
public class SemesterServiceImpl extends ServiceImpl<SemesterMapper, Semester> implements SemesterService {
    @Override
    public Result getList() {
        List<Semester> list = super.list();
        return Result.success("成功返回学期列表",list);
    }

    @Override
    public Result add(SemesterVo semesterVo) {
        Semester semester = new Semester();
        BeanUtils.copyProperties(semesterVo,semester);
        semester.setSemesterEndDate();
        // TODO 等待做jwt然后通过token拿到用户id
//        semester.setCreatedBy();
        boolean b = super.save(semester);
        return b ? Result.success("添加成功") : Result.error(400,"添加失败");
    }

    @Override
    public Result update(SemesterVo semesterVo) {
        Semester semester = super.getById(semesterVo.getId());
        if (semester == null){
            return Result.error(400,"该学期不存在");
        }
        BeanUtils.copyProperties(semesterVo,semester);
        semester.setSemesterEndDate();
        // TODO 等待做jwt然后通过token拿到用户id
//        semester.setUpdatedBy();
        boolean b = super.updateById(semester);
        return b ? Result.success("修改成功") : Result.error(400,"修改失败");
    }


    @Override
    public Result delete(Long id) {
        Semester semester = super.getById(id);
        if (semester == null){
            return Result.error(400,"该学期不存在");
        }
        // 物理删除
        boolean b = super.removeById(id);
        return b ? Result.success("删除成功") : Result.error(400,"删除失败");
    }
}
